package xj.graph2d.tree;

import java.awt.BorderLayout;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

import xj.app.LookAndFeelManager;
import xj.graph2d.DocumentChangeListener;
import xj.graph2d.DrawShape;
import xj.graph2d.app.GraphApp;

import static xj.app.BaseApplicationFrame.getResourceString;

public class TabbedProjectPanel extends JPanel implements
    DocumentChangeListener {

  public TabbedProjectPanel(GraphApp main, Map icons) {
    panes = new JPanel[2];
    labels = new String[2];
    tabIcons = new Icon[2];
    tabTips = new String[2];

    panes[0] = projPane = new ProjectPane(main, icons);
    labels[0] = getResourceString("ProjectTree.tab");
    tabTips[0] = getResourceString("ProjectTree.tip");
    panes[1] = new TabbedDocumentTreePanel(main, icons);
    labels[1] = getResourceString("DiagramTree.tab");
    tabTips[1] = getResourceString("DiagramTree.tip");

    JTabbedPane tabbedPane = new JTabbedPane();
    int n = 2;
    for (int i = 0; i < n; i++) {
      String tip = tabTips[i];
      if (tip != null) { 
	tip = "<html><center>" + tip.replaceAll("\n", "<br/>") + "</center></html>";
      } 
      tabbedPane.addTab(labels[i], tabIcons[i], panes[i], tip);
    }
    tabbedPane.setTabPlacement(SwingConstants.BOTTOM);
    LookAndFeelManager lnfManager = LookAndFeelManager.getInstance();
    if (lnfManager.isSubstanceLnF()) { 
      tabbedPane.setTabPlacement(SwingConstants.LEFT);
      //tabbedPane.putClientProperty(org.jvnet.substance.SubstanceLookAndFeel.TABBED_PANE_VERTICAL_ORIENTATION, true);
    }

    setLayout(new BorderLayout());
    add(tabbedPane, BorderLayout.CENTER);


  }

  public ProjectPane getProjectPane() {
    return projPane;
  }

  public void shapeChanged(DrawShape shape) {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).shapeChanged(shape);
    }
  }

  public void shapeSelected(DrawShape shape) {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).shapeSelected(shape);
    }
  }

  public void shapeAdded(DrawShape shape) {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).shapeAdded(shape);
    }
  }

  public void shapeRemoved(DrawShape shape) {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).shapeRemoved(shape);
    }
  }

  public void selectionSetUpdated() {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).selectionSetUpdated();
    }
  }

  public void clear() {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).clear();
    }
  }

  public void refresh() {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).refresh();
    }
  }

  public void reset() {
    if (panes[1] instanceof DocumentChangeListener) {
      ((DocumentChangeListener) panes[1]).reset();
    }
  }

  protected JPanel[] panes;

  protected String[] labels;

  protected Icon[] tabIcons;

  protected String[] tabTips;

  protected ProjectPane projPane;

}
